package com.zzptc.tkfate;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    List<Book> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.lv_bookfate);
        list=new ArrayList<>();
        //数据源
        Book book=new Book(1,R.mipmap.a1,"贫穷的木质",120);
        Book book1=new Book(2,R.mipmap.a2,"万物由来",150);
        Book book2=new Book(3,R.mipmap.a3,"乌合之众",100);
        Book book3=new Book(4,R.mipmap.a4,"神奇投车",125);
        Book book4=new Book(5,R.mipmap.a5,"彼之英雄",199);

        list.add(book);
        list.add(book1);
        list.add(book2);
        list.add(book3);
        list.add(book4);

        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);//将listview对象与适配器关联

    }
    //2.绑定数据
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //读出一条数据
            Book book=list.get(position);
            //绑定（先做小界面）
            View v=View.inflate(MainActivity.this,R.layout.item_book,null);

            ImageView iv_pic=v.findViewById(R.id.tv_book_pic);//找小界面的控件
            iv_pic.setImageResource(book.getBook_pic());//将数据绑定
            TextView tv_id=v.findViewById(R.id.tv_book_id);//找小界面的控件
            tv_id.setText(book.getBook_id()+"");//将数据绑定
            TextView tv_name=v.findViewById(R.id.tv_book_name);//找小界面的控件
            tv_name.setText(book.getBook_name());//将数据绑定
            TextView tv_price=v.findViewById(R.id.tv_book_price);//找小界面的控件
            tv_price.setText(book.getBook_price()+"");//将数据绑定



            return v;
        }
    }
}